﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RuleCode.Table;
using chatblog;

namespace map.member.order
{
    public partial class TradeRates : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            chatblog.Util.CheckBrokerOpr(this.Page);
            if (!IsPostBack)
            {
                InitPage();
            }
        }

        private void InitPage()
        {
            if (Request["type"] != null && Request["id"] != null)
            {
                string rateType = Request["type"];
                string tradeRecid = Request["id"];
                if (rateType == "seller")
                {
                    RateTypeStrLabel.Text = "卖家评价";
                    NickTypeLabel.Text  = "买家昵称";
                    this.Title = "卖家评价-藏宝图";
                }
                else
                {
                    RateTypeStrLabel.Text = "买家评价";
                    NickTypeLabel.Text = "卖家昵称";
                    this.Title = "买家评价-藏宝图";
                }
                CurrentUserLabel.Text = AliApi.appUserId;
                taobaotrade trade = taobaotrade.Find("recid="+tradeRecid);
                if (trade.Count > 0)
                {
                    ItemNameHyperLink.NavigateUrl = taobaouseritems.getitemlinkstringcbw(trade.taobaoiid.Value);
                    NickNameLabel.Text = trade.buyer_nick.Value;
                    ItemNameHyperLink.Text = trade.taobaoname .Value ;
                    //ItemNameHyperLink  设置宝贝链接
                    ItemCountLabel.Text = trade.taobaonum.Value.ToString ();
                    ItemPriceLabel.Text = trade.taobaoprice.Value.ToString();
                    TotalPayLabel.Text = trade.taobaopayment.Value.ToString();
                    PostTypeLabel.Text = trade.cbtrantype.Value;
                    TradeDateLabel.Text = trade.taobaocreated.Value;
                }
            }
        }

        protected void AddRateButton_Click(object sender, EventArgs e)
        {
            taobaotrade trade; 
            string rateType;
            string tradeRecid;
            if (Request["type"] != null && Request["id"] != null)
            {
                rateType = Request["type"];
                tradeRecid = Request["id"];
                trade = taobaotrade.Find("recid=" + tradeRecid);
                if (trade.Count <= 0 || rateType ==string.Empty || tradeRecid ==string.Empty )
                {
                    MessageBox.Show(this.Page, "页面参数不正确，您没有权限进行此操作！");
                    return;
                }
            }
            else
            {
                MessageBox.Show(this.Page ,"页面参数不正确，您没有权限进行此操作！");
                return;
            }

            if (AliApi.appUserId != string.Empty )
            {
                if (cbuser.ValidatePassword(CurrentUserLabel.Text, PasswordTextBox.Text) == false)
                {
                    MessageBox.Show(this.Page,"对不起，密码不正确！");
                    return;
                }


                cbuser u = cbuser.Find(" cbuserid = '" + AliApi.appUserId + "'");
                if (u.cbmoney.Value >= 1)
                {
                    lypingjia pingjia = lypingjia.Find("1=2");
                    pingjia.New();
                   pingjia.cbuseridby.Value = AliApi.appUserId;
                    if (rateType == "seller")
                    {
                        pingjia.cbuserid.Value = trade .buyer_nick.ToString() ;
                    }
                    else
                    {
                        pingjia.cbuserid.Value = trade.seller_nick.ToString();
                    }
                    pingjia.jcdesc2.Value = RateDescTextBox.Text;
                    pingjia.jccreatedate.Value = DateTime.Now;
                    pingjia.jccreatetime.Value = (int)DateTime.Now.TimeOfDay.TotalSeconds;
                    pingjia.cbstatus.Value = "未审核";
                    pingjia.cbtitle.Value = RateTitleTextBox.Text;
                    if (TotalPointRadioButtonList.SelectedValue == "好评")
                    {
                        pingjia.jcpoint.Value = 1;
                    }
                    else 
                    {
                        pingjia.jcpoint.Value = -1;
                    }
                    pingjia.cbcataid.Value = "综合";
                    pingjia.cbtype.Value = "taobaotrade";
                    pingjia.cbrelrecid.Value = Request["id"];
                    pingjia.Insert();
                    cbmoneytrans mt = cbmoneytrans.Find("1=2");
                    mt.cbmoneytrantype.Value = "评价操作";
                    mt.cbuserid.Value = AliApi.appUserId;
                    mt.jccreatedate.Value = DateTime.Now;
                    mt.jccreatetime.StringValue = DateTime.Now.ToLongTimeString();
                    mt.cbmoney.Value = -1;
                    mt.RefTable.Value = "lypingjia";
                    mt.RefRecID.Value = pingjia.RECID.ToString();
                    mt.Insert();
                    u.cbmoney.Value += -1;
                    u.cbmoneyF.Value += 1;
                    u.Modify();
                    if (rateType == "seller")
                    {
                        cbuser bu = cbuser.Find("cbuserid = '" + trade.buyer_nick.ToString() + "'");
                        if (TotalPointRadioButtonList.SelectedValue == "好评")
                        {
                           // bu.pingjia_good.Value += 1;
                        }
                        else
                        {
                           // bu.pingjia_bad.Value += 1;
                        }
                        bu.Modify();
                    }
                    else
                    {
                        cbuser bu = cbuser.Find("cbuserid = '" + trade.seller_nick.ToString() + "'");
                        if (TotalPointRadioButtonList.SelectedValue == "好评")
                        {
                           // bu.pingjia_good.Value += 1;
                        }
                        else
                        {
                           // bu.pingjia_bad.Value += 1;
                        }
                        bu.Modify();
                    }
                }
                else
                {
                    MessageBox.Show(this.Page , "你当前的账户余额不够");
                    return;
                }


                

                string redirectUrl = string.Empty;
                if (rateType == "seller")
                {
                    trade.taobaoseller_rate.Value = "true";
                    //trade.ratestatus.Value = (trade.ratestatus.Value == TRADE_RATE_STATUS.Buyer_Rate) ? TRADE_RATE_STATUS.Both_Rate : TRADE_RATE_STATUS.Seller_Rate;
                    redirectUrl = "sell_17.aspx";
                }
                else
                {
                    trade.taobaobuyer_rate.Value  = "true";
                    //trade.ratestatus.Value = (trade.ratestatus.Value == TRADE_RATE_STATUS.Seller_Rate) ? TRADE_RATE_STATUS.Both_Rate : TRADE_RATE_STATUS.Buyer_Rate;
                    redirectUrl = "buy_15.aspx";
                }
                trade.Modify();
                MessageBox.ShowAndRedirect(this.Page, "评价成功！", redirectUrl);
            }
            else 
            {
                MessageBox.ShowAndRedirect(this.Page, "对不起，您还未登录！", "/member/login.aspx?redirecturl="+Request.Url.ToString ());
            }
        }


        public string GetOtherUserId()
        {
            string otherUserid = string.Empty;
            if (Request["type"] != null && Request["id"] != null)
            {
                string rateType = Request["type"];
                string tradeRecid = Request["id"];
                taobaotrade trade = taobaotrade.Find("recid=" + tradeRecid);
                if (trade.Count > 0 && rateType != string.Empty)
                {
                    if (rateType == "seller")
                        otherUserid = trade.buyer_nick.Value;
                    else
                        otherUserid = trade.seller_nick.Value;
                }
            }
            return otherUserid;
        }
    }
}
